package org.oncase.smcase.action;

import java.util.HashMap;
import java.util.Map;

import org.oncase.smcase.controller.ObjectiveController;
import org.oncase.smcase.entity.Frequency;
import org.oncase.smcase.entity.FrequencyUnit;
import org.oncase.smcase.entity.Objective;

import com.google.inject.Inject;
import com.opensymphony.xwork2.ActionSupport;

public class ObjectiveAction extends ActionSupport {

	private static final long serialVersionUID = 1L;
	
	private String name;
	
	private String description;
	
	private String attribute;
	
	private String dataOrignDescription;
	
	private String criticalFactors;
	
	private String indicators;
	
	private String frequencyLength;
	
	private String frequencyUnit;
	
	private String targets;
	
	private String upperTolerance;
	
	private String lowerTolerance;
	
	private String action;
	
	private int objId;
	
	private Map<String, Object> ajaxResponse = new HashMap<String, Object>();
	
	private ObjectiveController objectiveCon;
	
	@Inject
	public ObjectiveAction(ObjectiveController objectiveCon) {
		this.objectiveCon = objectiveCon;
	}

	@Override
	public String execute() throws Exception {
		String returnView = ERROR;
		
		if (this.getName() != null && !this.getName().trim().equals("")) {
			if (this.getDescription() != null && !this.getDescription().trim().equals("")) {
				if (this.getIndicators() != null && !this.getIndicators().trim().equals("")) {
					if (this.getTargets() != null && !this.getTargets().trim().equals("")) {
						if (this.getFrequencyUnit() != null 
								&& this.getFrequencyLength().matches("^[1-9][0-9]*$")
								&& FrequencyUnit.valueOf(this.getFrequencyUnit().trim()) != null) {
							
							Frequency frequency = new Frequency(
									FrequencyUnit.valueOf(this.getFrequencyUnit().trim()),
									Integer.parseInt(this.getFrequencyLength()));
							
							Objective objective = null;
							
							if (action != null && action.equals("insert")) {
								
								objective = new Objective(this.getName().trim(), 
												this.getDescription().trim(), 
												this.getIndicators().trim(),
												frequency,
												this.getTargets().trim());
								
								this.setOptionalFields(objective);
								
								this.objectiveCon.insert(objective);
								
								this.ajaxResponse.put("objId", objective.getId());
								returnView = SUCCESS;
								
							} else if (action != null 
									&& action.equals("update")
									&& objId > 0) {
								
								objective = this.objectiveCon.findByPK(objId);
								objective.setName(this.getName().trim());
								objective.setDescription(this.getDescription().trim());
								objective.setIndicators(this.getIndicators().trim());
								objective.getFrequency().setLength(frequency.getLength());
								objective.getFrequency().setUnit(frequency.getUnit());
								objective.setTargets(this.getTargets().trim());
								
								frequency = objective.getFrequency();
								
								this.setOptionalFields(objective);
								
								this.objectiveCon.update(objective);
								
								this.ajaxResponse.put("objId", objective.getId());
								returnView = SUCCESS;
							} else {
								this.ajaxResponse.put(ERROR, "The objective is not found.");
							}
						} else {
							this.ajaxResponse.put(ERROR, "The field frequency is required.");
						}
					} else {
						this.ajaxResponse.put(ERROR, "The field targets is required.");
					}
				} else {
					this.ajaxResponse.put(ERROR, "The field indicators is required.");
				}
			} else {
				this.ajaxResponse.put(ERROR, "The field description is required.");
			}
		} else {
			this.ajaxResponse.put(ERROR, "The field name is required.");
		}
		
		return returnView;
	}

	private void setOptionalFields(Objective objective) {
		if (this.getAttribute() != null && !this.getAttribute().trim().equals("")) {
			objective.setAttribute(this.getAttribute());
		}
		
		if (this.getDataOrignDescription() != null 
				&& !this.getDataOrignDescription().trim().equals("")) {
			objective.setDataOrignDescription(this.getDataOrignDescription());
		}
		
		if (this.getCriticalFactors() != null 
				&& !this.getCriticalFactors().trim().equals("")) {
			objective.setCriticalFactors(this.getCriticalFactors());
		}
		
		if (this.getUpperTolerance() != null 
				&& !this.getUpperTolerance().trim().equals("")) {
			objective.setUpperTolerance(this.getUpperTolerance());
		}
		
		if (this.getLowerTolerance() != null 
				&& !this.getLowerTolerance().trim().equals("")) {
			objective.setLowerTolerance(this.getLowerTolerance());
		}
	}

	public String getAction() {
		return action;
	}

	public void setAction(String action) {
		this.action = action;
	}

	public int getObjId() {
		return objId;
	}

	public void setObjId(int objId) {
		this.objId = objId;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public String getAttribute() {
		return attribute;
	}

	public void setAttribute(String attribute) {
		this.attribute = attribute;
	}

	public String getDataOrignDescription() {
		return dataOrignDescription;
	}

	public void setDataOrignDescription(String dataOrignDescription) {
		this.dataOrignDescription = dataOrignDescription;
	}

	public String getCriticalFactors() {
		return criticalFactors;
	}

	public void setCriticalFactors(String criticalFactors) {
		this.criticalFactors = criticalFactors;
	}

	public String getIndicators() {
		return indicators;
	}

	public void setIndicators(String indicators) {
		this.indicators = indicators;
	}

	public String getFrequencyLength() {
		return frequencyLength;
	}

	public void setFrequencyLength(String frequencyLength) {
		this.frequencyLength = frequencyLength;
	}

	public String getFrequencyUnit() {
		return frequencyUnit;
	}

	public void setFrequencyUnit(String frequencyUnit) {
		this.frequencyUnit = frequencyUnit;
	}

	public String getTargets() {
		return targets;
	}

	public void setTargets(String targets) {
		this.targets = targets;
	}

	public String getUpperTolerance() {
		return upperTolerance;
	}

	public void setUpperTolerance(String upperTolerance) {
		this.upperTolerance = upperTolerance;
	}

	public String getLowerTolerance() {
		return lowerTolerance;
	}

	public void setLowerTolerance(String lowerTolerance) {
		this.lowerTolerance = lowerTolerance;
	}

	public Map<String, Object> getAjaxResponse() {
		return ajaxResponse;
	}

	public void setAjaxResponse(Map<String, Object> ajaxResponse) {
		this.ajaxResponse = ajaxResponse;
	}

}
